package com.zs.zcerp.system.controller;

import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.entity.QhChkmgrdEntity;
import com.zs.zcerp.approval.entity.QhbillstdmEntity;
import com.zs.zcerp.approval.service.IQhBasestdMService;
import com.zs.zcerp.approval.service.IQhbillstdmService;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.utils.DateUtil;
import com.zs.zcerp.common.utils.FebsUtil;
import com.zs.zcerp.entrance.entity.Syorg;
import com.zs.zcerp.entrance.service.ISyorgService;
import com.zs.zcerp.system.entity.User;
import com.zs.zcerp.system.service.IUserDataPermissionService;
import com.zs.zcerp.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.ExpiredSessionException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 */
@Controller("systemView")
@RequiredArgsConstructor
public class ViewController extends BaseController {

    private final IUserService userService;
    private final IUserDataPermissionService userDataPermissionService;
    private final IQhBasestdMService iQhBasestdMService;
    private final IQhbillstdmService iQhbillstdmService;
    private final ISyorgService iSyorgService;

    @GetMapping("login")
    @ResponseBody
    public Object login(HttpServletRequest request) {
        if (FebsUtil.isAjaxRequest(request)) {
            throw new ExpiredSessionException();
        } else {
            ModelAndView mav = new ModelAndView();
            mav.setViewName(FebsUtil.view("login"));
            return mav;
        }
    }

    @GetMapping("unauthorized")
    public String unauthorized() {
        return FebsUtil.view("error/403");
    }


    @GetMapping("/")
    public String redirectIndex() {
        return "redirect:/index";
    }

    @GetMapping("index")
    public String index(Model model) {
        User principal = userService.findByName(getCurrentUser().getUsername());
        userService.doGetUserAuthorizationInfo(principal);
        principal.setPassword("It's a secret");
        model.addAttribute("user", principal);
        model.addAttribute("permissions", principal.getStringPermissions());
        model.addAttribute("roles", principal.getRoles());
        return "index";
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "layout")
    public String layout() {
        return FebsUtil.view("layout");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "password/update")
    public String passwordUpdate() {
        return FebsUtil.view("system/user/passwordUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/profile")
    public String userProfile() {
        return FebsUtil.view("system/user/userProfile");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/avatar")
    public String userAvatar() {
        return FebsUtil.view("system/user/avatar");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "user/profile/update")
    public String profileUpdate() {
        return FebsUtil.view("system/user/profileUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user")
    @RequiresPermissions("user:view")
    public String systemUser() {
        return FebsUtil.view("system/user/user");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/add")
    @RequiresPermissions("user:add")
    public String systemUserAdd() {
        return FebsUtil.view("system/user/userAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/detail/{username}")
    @RequiresPermissions("user:view")
    public String systemUserDetail(@PathVariable String username, Model model) {
        resolveUserModel(username, model, true);
        return FebsUtil.view("system/user/userDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/user/update/{username}")
    @RequiresPermissions("user:update")
    public String systemUserUpdate(@PathVariable String username, Model model) {
        resolveUserModel(username, model, false);
        return FebsUtil.view("system/user/userUpdate");
    }
    /**  签核系统：前端mapping映射      begin    */
    /**
     * 基本流程
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/baseflow")
    @RequiresPermissions("baseflow:view")
    public String approvalBaseflow() {
        return FebsUtil.view("approval/baseflow/baseflow");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/baseflow/add")
    @RequiresPermissions("baseflow:add")
    public String addBase() {
        return FebsUtil.view("approval/baseflow/baseflowAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/baseflow/update/{basestdNo}")
    @RequiresPermissions("baseflow:update")
    public String approvalBaseflowUpdate(@PathVariable String basestdNo, Model model) {
        resolveBaseModel(basestdNo, model);
        return FebsUtil.view("approval/baseflow/baseflowUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/baseflow/detail/{basestdNo}")
    @RequiresPermissions("baseflow:view")
    public String approvalBaseflowDetail(@PathVariable String basestdNo, Model model) {
        resolveBaseModel(basestdNo, model);
        return FebsUtil.view("approval/baseflow/baseflowDetail");
    }
    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/baseflow/baseflowSign")
    @RequiresPermissions("baseStd:view")
    public String approvalBaseflowSign() {
        return FebsUtil.view("approval/baseflow/baseflowSign");
    }

   /* 
     * 流程单据分类
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/classify")
    @RequiresPermissions("classify:view")
    public String approvalClassify() {
        return FebsUtil.view("approval/classify/classify");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/classify/add")
    @RequiresPermissions("classify:add")
    public String approvalClassifyAdd() {
        return FebsUtil.view("approval/classify/classifyAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/classify/update/{billstdNo}")
    @RequiresPermissions("classify:update")
    public String approvalClassifyUpdate(@PathVariable String billstdNo, Model model) {
        QhbillstdmEntity  qhbillstdmEntity   = iQhbillstdmService.findByBasestdNo(billstdNo);
        model.addAttribute("qhbillstdmEntity", qhbillstdmEntity);
        return FebsUtil.view("approval/classify/classifyUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/classify/detail/{billstdNo}")
    @RequiresPermissions("classify:view")
    public String approvalapprovalClassifyUpdateDetail(@PathVariable String billstdNo, Model model) {
        QhbillstdmEntity  qhbillstdmEntity   = iQhbillstdmService.findByBasestdNo(billstdNo);
        model.addAttribute("qhbillstdmEntity", qhbillstdmEntity);
        return FebsUtil.view("approval/classify/classifyDetail");
    }


    /** 单据签核作业 */

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals")
    @RequiresPermissions("approvals:view")
    public String approvalApprovals() {
        return FebsUtil.view("approval/approvals/approvals");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/add")
    @RequiresPermissions("approvals:add")
    public String approvalApprovalsAdd() {
        return FebsUtil.view("approval/approvals/approvalsAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/sign")
    @RequiresPermissions("approvals:sign")
    public String approvalApprovalsUpdate(@PathVariable String orderNo, Model model) {
        model.addAttribute("orderNo", orderNo);
        return FebsUtil.view("approval/approvals/approvals");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/signDetails")
    @RequiresPermissions("approvals:view")
    public String signDetail() {
        return FebsUtil.view("approval/approvals/flow");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/approvalsDetailTable")
    @RequiresPermissions("approvals:view")
    public String detailTable() {
        return FebsUtil.view("approval/approvals/approvalsDetailTable");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/flow")
    @RequiresPermissions("approvals:view")
    public String flow() {
        return FebsUtil.view("approval/approvals/flow");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/tableList")
    // @RequiresPermissions("approvals:tableList")
    public String tableList() {
        return FebsUtil.view("approval/approvals/tableList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/approvalsDetail")
    public String approvalsDetail() {
        return FebsUtil.view("approval/approvals/approvalsDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/approvalsDetails")
    public String approvalsDetails() {
        return FebsUtil.view("approval/approvals/approvalsDetails");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/reject")
    @RequiresPermissions("reject:add")
    public String reject() {
        return FebsUtil.view("approval/approvals/approvalsReject");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/reject")
    @RequiresPermissions("reject:view")
    public String rejectList() {
        return FebsUtil.view("approval/approvals/approvalsRejectList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/approvalsRejectDetail")
    @RequiresPermissions("reject:view")
    public String rejectDetail() {
                return FebsUtil.view("approval/approvals/approvalsRejectDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/approvalsSign")
    @RequiresPermissions("approvals:sign")
    public String approvalsSign() {
        return FebsUtil.view("approval/approvals/approvalsSign");
    }


    /*
    /**报表
     */
    @PostMapping(FebsConstant.VIEW_PREFIX + "approval/approvals/report")
    @RequiresPermissions("approvals:view")
    public String reportApprovalsUpdate(@PathVariable QhChkmgrdEntity qhChkmgrdEntity, Model model) {
        return FebsUtil.view("approval/approvals/approvalDetail");
    }



    /** 签核状态查询 */

    @GetMapping(FebsConstant.VIEW_PREFIX + "approval/history")
    @RequiresPermissions("history:view")
    public String signList() {
        return FebsUtil.view("approval/history/signList");
    }


    /**
     * 签核系统：前端mapping映射  end
     */

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/role")
    @RequiresPermissions("role:view")
    public String systemRole() {
        return FebsUtil.view("system/role/role");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/menu")
    @RequiresPermissions("menu:view")
    public String systemMenu() {
        return FebsUtil.view("system/menu/menu");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/dept")
    @RequiresPermissions("dept:view")
    public String systemDept() {
        return FebsUtil.view("system/dept/dept");
    }

    @RequestMapping(FebsConstant.VIEW_PREFIX + "index")
    public String pageIndex() {
        return FebsUtil.view("index");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "404")
    public String error404() {
        return FebsUtil.view("error/404");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "403")
    public String error403() {
        return FebsUtil.view("error/403");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "500")
    public String error500() {
        return FebsUtil.view("error/500");
    }


    /**  系统评分：页面请求      begin    */

    /**
     * 系统评分
     *
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "system/remark")
    public String remarkIndex() {
        return FebsUtil.view("remark/remarkIndex");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "system/remark/{ppsNo}")
    public String remarkDetail(Model model, @PathVariable("ppsNo") String ppsNo) {
        return FebsUtil.view("remark/remarkDetail");
    }

    /**
     * 提案
     *
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "system/proposal")
    public String proposal() {
        return FebsUtil.view("remark/proposal");
    }

    /**
     * 提案新增与修改
     *
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "system/proposal/edit")
    public String proposalEdit() {
        return FebsUtil.view("remark/proposalEdit");
    }

    /**
     * 提案详情
     *
     * @return
     */
    @GetMapping(FebsConstant.VIEW_PREFIX + "system/proposal/detail")
    public String proposalDetail() {
        return FebsUtil.view("remark/proposalDetail");
    }

    /**
     * 系统评分：页面请求  end
     */

    private void resolveUserModel(String username, Model model, Boolean transform) {
        User user = userService.findByName(username);
        String deptIds = userDataPermissionService.findByUserId(String.valueOf(user.getUserId()));
        user.setDeptIds(deptIds);
        model.addAttribute("user", user);
        if (transform) {
            String sex = user.getSex();
            switch (sex) {
                case User.SEX_MALE:
                    user.setSex("男");
                    break;
                case User.SEX_FEMALE:
                    user.setSex("女");
                    break;
                default:
                    user.setSex("保密");
                    break;
            }
        }
        if (user.getLastLoginTime() != null) {
            model.addAttribute("lastLoginTime", DateUtil.getDateFormat(user.getLastLoginTime(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
    }

    private void resolveBaseModel(String basestdNo, Model model) {
        QhBasestdMEntity qhBasestdMEntity = iQhBasestdMService.findByBasestdNo(basestdNo);
        model.addAttribute("qhBasestdMEntity", qhBasestdMEntity);
        if (qhBasestdMEntity != null) {
            QhbillstdmEntity qhbillstdmEntity = iQhbillstdmService.findByBasestdNo(qhBasestdMEntity.getBillstdNo());
            if (qhbillstdmEntity != null) {
                qhBasestdMEntity.setBillstdName(qhbillstdmEntity.getBillstdNm());
                qhBasestdMEntity.setBillstdNo(qhbillstdmEntity.getBillstdNo());
            }else{
                qhBasestdMEntity.setBillstdName("单据无名字");
            }
            Syorg syorg  = iSyorgService.getSyorg(qhBasestdMEntity.getOrgId());
            if (syorg != null) {
                qhBasestdMEntity.setOrgNameT(syorg.getShortnmT());
            }else{
                qhBasestdMEntity.setOrgNameT("无中文名字联系管理员");
            }

        }

    }

}
